<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>vue组件（九) | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Vue,">
  

  <meta name="description" content="一、组件的基本使用1.1 注册组件 注册组件就是利用Vue.component()方法，先传入一个自定义组件的名字，然后传入这个组件的配置  Vue.component(&apos;mycomponent&apos;,&amp;#123;    template: `&amp;lt;div&amp;gt;这是一个自定义组件&amp;lt;/div&amp;gt;`,    data () &amp;#123;      return &amp;#123;        m">
<meta name="keywords" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue组件（九)">
<meta property="og:url" content="http://blog.poetries.top/2018/08/27/vue-component/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、组件的基本使用1.1 注册组件 注册组件就是利用Vue.component()方法，先传入一个自定义组件的名字，然后传入这个组件的配置  Vue.component(&apos;mycomponent&apos;,&amp;#123;    template: `&amp;lt;div&amp;gt;这是一个自定义组件&amp;lt;/div&amp;gt;`,    data () &amp;#123;      return &amp;#123;        m">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/621.png">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/1480597-a057513f8294a129.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2020-08-15T04:25:31.938Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue组件（九)">
<meta name="twitter:description" content="一、组件的基本使用1.1 注册组件 注册组件就是利用Vue.component()方法，先传入一个自定义组件的名字，然后传入这个组件的配置  Vue.component(&apos;mycomponent&apos;,&amp;#123;    template: `&amp;lt;div&amp;gt;这是一个自定义组件&amp;lt;/div&amp;gt;`,    data () &amp;#123;      return &amp;#123;        m">
<meta name="twitter:image" content="https://poetries1.gitee.io/img-repo/2019/10/621.png">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、组件的基本使用"><span class="toc-text">一、组件的基本使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-注册组件"><span class="toc-text">1.1 注册组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-模板的要求"><span class="toc-text">1.2 模板的要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-组件中的data必须是函数"><span class="toc-text">1.3 组件中的data必须是函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-关于DOM模板的解析"><span class="toc-text">1.4 关于DOM模板的解析</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、组件的属性和事件"><span class="toc-text">二、组件的属性和事件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、属性Props"><span class="toc-text">三、属性Props</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-v-bind绑定属性值"><span class="toc-text">3.1 v-bind绑定属性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-动态绑定特性值"><span class="toc-text">3.2 动态绑定特性值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-子组件希望对传入的prop进行操作"><span class="toc-text">3.3 子组件希望对传入的prop进行操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-给子组件传递正确类型的值"><span class="toc-text">3.4 给子组件传递正确类型的值</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、Prop验证"><span class="toc-text">四、Prop验证</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、自定义事件"><span class="toc-text">五、自定义事件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-绑定原生事件"><span class="toc-text">5.1 绑定原生事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-探究v-model"><span class="toc-text">5.2 探究v-model</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3-使用自定义事件创建自定义的表单输入组件"><span class="toc-text">5.3 使用自定义事件创建自定义的表单输入组件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、动态组件"><span class="toc-text">六、动态组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#七、使用slot分发内容"><span class="toc-text">七、使用slot分发内容</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#7-1-单个slot"><span class="toc-text">7.1 单个slot</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-2-带名称的slot"><span class="toc-text">7.2 带名称的slot</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#八、组件通信"><span class="toc-text">八、组件通信</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-1-父与子"><span class="toc-text">8.1 父与子</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-2-子与父"><span class="toc-text">8.2 子与父</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-3-兄弟组件之间的通信"><span class="toc-text">8.3 兄弟组件之间的通信</span></a></li></ol></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-vue-component" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">vue组件（九)</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.08.27</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 5.1k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 21分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、组件的基本使用"><a href="#一、组件的基本使用" class="headerlink" title="一、组件的基本使用"></a>一、组件的基本使用</h2><h3 id="1-1-注册组件"><a href="#1-1-注册组件" class="headerlink" title="1.1 注册组件"></a>1.1 注册组件</h3><blockquote>
<p>注册组件就是利用<code>Vue.component()</code>方法，先传入一个自定义组件的名字，然后传入这个组件的配置</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'mycomponent'</span>,&#123;</span><br><span class="line">    template: <span class="string">`&lt;div&gt;这是一个自定义组件&lt;/div&gt;`</span>,</span><br><span class="line">    data () &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        message: <span class="string">'hello world'</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如上方式，就已经创建了一个自定义组件，然后就可以在Vue实例挂在的DOM元素中使用它</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mycomponent</span>&gt;</span><span class="tag">&lt;/<span class="name">mycomponent</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">my-component</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">var</span> app = <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">    el: <span class="string">'#app'</span>,</span></span><br><span class="line">    data: &#123;</span><br><span class="line">    &#125;,</span><br><span class="line">    components: &#123;</span><br><span class="line"><span class="javascript">      <span class="string">'my-component'</span>: &#123;</span></span><br><span class="line"><span class="xml">        template: `<span class="tag">&lt;<span class="name">div</span>&gt;</span>这是一个局部的自定义组件，只能在当前Vue实例中使用<span class="tag">&lt;/<span class="name">div</span>&gt;</span>`,</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>直接使用<code>Vue.component()</code>创建的组件，所有的Vue实例都可以使用。还可以在某个Vue实例中注册只有自己能使用的组件</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    el: <span class="string">'#app'</span>,</span><br><span class="line">    data: &#123;</span><br><span class="line">    &#125;,</span><br><span class="line">    components: &#123;</span><br><span class="line">      <span class="string">'my-component'</span>: &#123;</span><br><span class="line">        template: <span class="string">`&lt;div&gt;这是一个局部的自定义组件，只能在当前Vue实例中使用&lt;/div&gt;`</span>,</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>
<h3 id="1-2-模板的要求"><a href="#1-2-模板的要求" class="headerlink" title="1.2 模板的要求"></a>1.2 模板的要求</h3><blockquote>
<p>注意：组件的模板只能有一个根元素。下面的情况是不允许的</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">template: `&lt;div&gt;这是一个局部的自定义组件，只能在当前Vue实例中使用&lt;/div&gt;</span><br><span class="line">            &lt;button&gt;hello&lt;/button&gt;`,</span><br></pre></td></tr></table></figure>
<h3 id="1-3-组件中的data必须是函数"><a href="#1-3-组件中的data必须是函数" class="headerlink" title="1.3 组件中的data必须是函数"></a>1.3 组件中的data必须是函数</h3><ul>
<li>可以看出，注册组件时传入的配置和创建Vue实例差不多，但也有不同，其中一个就是<code>data</code>属性必须是一个函数</li>
<li>这是因为如果像<code>Vue</code>实例那样，传入一个对象，由于JS中对象类型的变量实际上保存的是对象的引用，所以当存在多个这样的组件时，会共享数据，导致一个组件中数据的改变会引起其他组件数据的改变。</li>
<li>而使用一个返回对象的函数，每次使用组件都会创建一个新的对象，这样就不会出现共享数据的问题来了</li>
</ul>
<h3 id="1-4-关于DOM模板的解析"><a href="#1-4-关于DOM模板的解析" class="headerlink" title="1.4 关于DOM模板的解析"></a>1.4 关于DOM模板的解析</h3><blockquote>
<p>当使用 DOM 作为模版时 (例如，将 el 选项挂载到一个已存在的元素上), 你会受到 HTML 的一些限制，因为 Vue 只有在浏览器解析和标准化 HTML 后才能获取模板内容。尤其像这些元素 <code>&lt;ul&gt;</code>，<code>&lt;ol&gt;</code>，<code>&lt;table&gt;</code>，<code>&lt;select&gt;</code> 限制了能被它包裹的元素，而一些像 <code>&lt;option&gt;</code> 这样的元素只能出现在某些其它元素内部</p>
</blockquote>
<ul>
<li>在自定义组件中使用这些受限制的元素时会导致一些问题，例如</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">table</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">my-row</span>&gt;</span>...<span class="tag">&lt;/<span class="name">my-row</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>自定义组件 <code>&lt;my-row&gt;</code> 被认为是无效的内容，因此在渲染的时候会导致错误。这时应使用特殊的 <code>is</code> 属性</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">table</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">tr</span> <span class="attr">is</span>=<span class="string">"my-row"</span>&gt;</span><span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>也就是说，标准HTML中，一些元素中只能放置特定的子元素，另一些元素只能存在于特定的父元素中。比如<code>table</code>中不能放置<code>div</code>，<code>tr</code>的父元素不能<code>div</code>等。所以，当使用自定义标签时，标签名还是那些标签的名字，但是可以在标签的<code>is</code>属性中填写自定义组件的名字</p>
</blockquote>
<p><strong>应当注意，如果您使用来自以下来源之一的字符串模板，这些限制将不适用</strong>：</p>
<ul>
<li><code>&lt;script type=&quot;text/x-template&quot;&gt;</code></li>
<li><code>JavaScript</code> 内联模版字符串</li>
<li><code>.vue</code> 组件</li>
</ul>
<blockquote>
<p>一般情况下，只有单文件组件<code>.vue</code>可以忽略这种情况</p>
</blockquote>
<h2 id="二、组件的属性和事件"><a href="#二、组件的属性和事件" class="headerlink" title="二、组件的属性和事件"></a>二、组件的属性和事件</h2><blockquote>
<p>在<code>html</code>中使用元素，会有一些属性，如<code>class</code>,<code>id</code>，还可以绑定事件，自定义组件也是可以的。当在一个组件中，使用了其他自定义组件时，就会利用子组件的属性和事件来和父组件进行数据交流</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/621.png" alt></p>
<ul>
<li>父子组件之间的通信就是 <code>props down</code>,<code>events up</code>，父组件通过属性<code>props</code>向下传递数据给子组件，子组件通过 事件<code>events</code> 给父组件发送消息。</li>
<li>比如，子组件需要某个数据，就在内部定义一个prop属性，然后父组件就像给<code>html</code>元素指定特性值一样，把自己的<code>data</code>属性传递给子组件的这个属性。</li>
<li>而当子组件内部发生了什么事情的时候，就通过自定义事件来把这个事情涉及到的数据暴露出来，供父组件处理</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">my-component</span> <span class="attr">v-bind:foo</span>=<span class="string">"baz"</span> <span class="attr">v-on:event-a</span>=<span class="string">"doThis(arg1,...arg2)"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>foo</code>是<code>&lt;my-component&gt;</code>组件内部定义的一个<code>prop</code>属性，<code>baz</code>是父组件的一个<code>data</code>属性，</li>
<li><code>event-a</code>是子组件定义的一个事件，<code>doThis</code>是父组件的一个方法</li>
</ul>
<p><strong>过程就是这样</strong></p>
<ul>
<li>父组件把<code>baz</code>数据通过<code>prop</code>传递给子组件的<code>foo</code>；</li>
<li>子组件内部得到<code>foo</code>的值，就可以进行相应的操作；</li>
<li>当子组件内部发生了一些变化，希望父组件能知道时，就利用代码触发<code>event-a</code>事件，把一些数据发送出去</li>
<li>父组件把这个事件处理器绑定为<code>doThis</code>方法，子组件发送的数据，就作为<code>doThis</code>方法的参数被传进来</li>
<li>然后父组件就可以根据这些数据，进行相应的操作</li>
</ul>
<h2 id="三、属性Props"><a href="#三、属性Props" class="headerlink" title="三、属性Props"></a>三、属性Props</h2><blockquote>
<p>Vue组件通过<code>props</code>属性来声明一个自己的属性，然后父组件就可以往里面传递数据</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'mycomponent'</span>,&#123;</span><br><span class="line">    template: <span class="string">'&lt;div&gt;这是一个自定义组件,父组件传给我的内容是：&#123;&#123;myMessage&#125;&#125;&lt;/div&gt;'</span>,</span><br><span class="line">    props: [<span class="string">'myMessage'</span>],</span><br><span class="line">    data () &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        message: <span class="string">'hello world'</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>调用该组件</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mycomponent</span> <span class="attr">my-message</span>=<span class="string">"hello"</span>&gt;</span><span class="tag">&lt;/<span class="name">mycomponent</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li><strong>注意</strong>，由于<code>HTML</code>特性是不区分大小写的，所以传递属性值时，<code>myMessage</code>应该转换成 <code>kebab-case</code> (短横线隔开式)<code>my-message=&quot;hello&quot;</code></li>
</ul>
<h3 id="3-1-v-bind绑定属性值"><a href="#3-1-v-bind绑定属性值" class="headerlink" title="3.1 v-bind绑定属性值"></a>3.1 v-bind绑定属性值</h3><ul>
<li>一般情况下，使用<code>v-bind</code>给元素特性(attribute)传递值时，<code>Vue</code>会将<code>&quot;&quot;</code>中的内容当做一个表达式</li>
<li>用<code>v-bind:class</code>和<code>class</code>传入正常的类名，效果是一样的，因为对于这两个特性，<code>Vue</code>采用了合并而不是替换的原则</li>
</ul>
<h3 id="3-2-动态绑定特性值"><a href="#3-2-动态绑定特性值" class="headerlink" title="3.2 动态绑定特性值"></a>3.2 动态绑定特性值</h3><blockquote>
<p>想要把父组件的属性绑定到子组件，应该使用<code>v-bind</code>，这样，父组件中数据改变时能反映到子组件。<br>注意，根据父组件传递给子组件的属性类型的不同，当在子组件中更改这个属性时，会有以下两种情况</p>
</blockquote>
<ul>
<li>当父组件传递的属性是引用类型时，在子组件中更改相应的属性会导致父组件相应属性的更改</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app2"</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">div</span>&gt;</span>这是父组件的parentArray：&#123;&#123;parentArray&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">my-component</span> <span class="attr">:child-array</span>=<span class="string">"parentArray"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">     Vue.component(<span class="string">'my-component'</span>, &#123;</span></span><br><span class="line">       template: `</span><br><span class="line"><span class="xml">       <span class="tag">&lt;<span class="name">div</span>&gt;</span>这是接收了父组件传递值的子组件的childArray: &#123;&#123;childArray&#125;&#125; <span class="tag">&lt;<span class="name">br</span>&gt;</span></span></span><br><span class="line"><span class="xml">           <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span> @<span class="attr">click</span>=<span class="string">"changeArray"</span>&gt;</span></span></span><br><span class="line"><span class="xml">           点击我改变父元素的parentArray<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">         <span class="tag">&lt;/<span class="name">div</span>&gt;</span>`,</span></span><br><span class="line"><span class="javascript">       props: [<span class="string">'childArray'</span>],</span></span><br><span class="line">       data () &#123;</span><br><span class="line"><span class="javascript">         <span class="keyword">return</span> &#123;</span></span><br><span class="line">           counter: 1</span><br><span class="line">         &#125;</span><br><span class="line">       &#125;,</span><br><span class="line">       methods: &#123;</span><br><span class="line">         changeArray () &#123;</span><br><span class="line"><span class="javascript">           <span class="keyword">this</span>.childArray.push(<span class="keyword">this</span>.counter++)</span></span><br><span class="line">         &#125;</span><br><span class="line">       &#125;</span><br><span class="line">     &#125;)</span><br><span class="line"><span class="javascript">     <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">       el: <span class="string">'#app2'</span>,</span></span><br><span class="line">       data: &#123;</span><br><span class="line">         parentArray: []</span><br><span class="line">       &#125;</span><br><span class="line">     &#125;)</span><br><span class="line">   <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>当父组件传递值为基本类型时，在子组件中更改这个属性会报错。正确的做法是，在父组件中绑定属性值时，加上<code>.sync</code>修饰符</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">my-component</span> <span class="attr">:child-array.sync</span>=<span class="string">"parentArray"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>然后在子组件中改变相应的属性</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">methods: &#123;</span><br><span class="line">    changeArray () &#123;</span><br><span class="line">      <span class="keyword">this</span>.counter++</span><br><span class="line">      <span class="keyword">this</span>.$emit(<span class="string">'update:childArray'</span>, <span class="keyword">this</span>.counter)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h3 id="3-3-子组件希望对传入的prop进行操作"><a href="#3-3-子组件希望对传入的prop进行操作" class="headerlink" title="3.3 子组件希望对传入的prop进行操作"></a>3.3 子组件希望对传入的prop进行操作</h3><blockquote>
<p>一般来说，是不建议在子组件中对父组件中传递来的属性进行操作的。如果真的有这种需求,可以这样:</p>
</blockquote>
<ul>
<li>父组件传递了一个基本类型值，那么可以在子组件中创建一个新的属性，并以传递进来的值进行初始化，之后就可以操作这个新的属性了</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">props: [<span class="string">'initialCounter'</span>],</span><br><span class="line">data: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123; <span class="attr">counter</span>: <span class="keyword">this</span>.initialCounter &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>父组件传递了一个引用类型值，为了避免更改父组件中相应的数据，最好是对引用类型进行复制。复杂的情况，肯定应该是深复制。</li>
</ul>
<h3 id="3-4-给子组件传递正确类型的值"><a href="#3-4-给子组件传递正确类型的值" class="headerlink" title="3.4 给子组件传递正确类型的值"></a>3.4 给子组件传递正确类型的值</h3><blockquote>
<p>静态的给子组件的特性传递值，它都会把他当做一个字符串</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;!-- 传递了一个字符串 &quot;1&quot; --&gt;</span><br><span class="line">&lt;comp some-prop=&quot;1&quot;&gt;&lt;/comp&gt;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>子组件中，特性的值是字符串 “1” 而不是 <code>number</code> 1。如果想传递正确的数值，应该使用<code>v-bind</code>传递，这样就能把传递的值当做一个表达式来处理，而不是字符串</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;!-- 传递实际的 number 1 --&gt;</span><br><span class="line">&lt;comp v-bind:some-prop=&quot;1&quot;&gt;&lt;/comp&gt;</span><br></pre></td></tr></table></figure>
<h2 id="四、Prop验证"><a href="#四、Prop验证" class="headerlink" title="四、Prop验证"></a>四、Prop验证</h2><blockquote>
<p>我们可以给组件的<code>props</code>属性添加验证，当传入的数据不符合要求时，<code>Vue</code>会发出警告</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">Vue.component(<span class="string">'example'</span>, &#123;</span><br><span class="line">  props: &#123;</span><br><span class="line">    <span class="comment">// 基础类型检测 (`null` 意思是任何类型都可以)</span></span><br><span class="line">    propA: <span class="built_in">Number</span>,</span><br><span class="line">    <span class="comment">// 多种类型</span></span><br><span class="line">    propB: [<span class="built_in">String</span>, <span class="built_in">Number</span>],</span><br><span class="line">    <span class="comment">// 必传且是字符串</span></span><br><span class="line">    propC: &#123;</span><br><span class="line">      type: <span class="built_in">String</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 数字，有默认值</span></span><br><span class="line">    propD: &#123;</span><br><span class="line">      type: <span class="built_in">Number</span>,</span><br><span class="line">      <span class="keyword">default</span>: <span class="number">100</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 数组/对象的默认值应当由一个工厂函数返回</span></span><br><span class="line">    propE: &#123;</span><br><span class="line">      type: <span class="built_in">Object</span>,</span><br><span class="line">      <span class="keyword">default</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">message</span>: <span class="string">'hello'</span> &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 自定义验证函数</span></span><br><span class="line">    propF: &#123;</span><br><span class="line">      validator: <span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> value &gt; <span class="number">10</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p><strong>type 可以是下面原生构造器</strong>：</p>
<ul>
<li><code>String</code></li>
<li><code>Number</code></li>
<li><code>Boolean</code></li>
<li><code>Function</code></li>
<li><code>Object</code></li>
<li><code>Array</code></li>
<li><code>Symbol</code></li>
<li><code>type</code>也可以是一个自定义构造器函数，使用 <code>instanceof</code> 检测</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 自定义Person构造器</span></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name, age</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name</span><br><span class="line">    <span class="keyword">this</span>.age = age</span><br><span class="line">  &#125;</span><br><span class="line">  Vue.component(<span class="string">'my-component'</span>, &#123;</span><br><span class="line">    template: <span class="string">`&lt;div&gt;名字: &#123;&#123; person-prop.name &#125;&#125;， 年龄： &#123;&#123; person-prop.age &#125;&#125; &lt;/div&gt;`</span>,</span><br><span class="line">    props: &#123;</span><br><span class="line">      person-prop: &#123;</span><br><span class="line">        type: Person     <span class="comment">// 指定类型</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    el: <span class="string">'#app2'</span>,</span><br><span class="line">    data: &#123;</span><br><span class="line">      person: <span class="number">2</span>        <span class="comment">// 传入Number类型会报错</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>
<p><strong>非Prop类型的属性</strong></p>
<blockquote>
<p>也可以像在html标签中添加<code>data-</code>开头的自定义属性一样，给自定义组件添加任意的属性。而不仅限于<code>data-*</code>形式，这样做的话，Vue会把这个属性放在自定义组件的根元素上。一个自定义组件的模板只能有一个根元素</p>
</blockquote>
<p><strong>覆盖非Prop属性</strong></p>
<blockquote>
<p>如果父组件向子组件的非<code>prop</code>属性传递了值，那么这个值会覆盖子组件模板中的特性</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app3"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">my-component2</span> <span class="attr">att</span>=<span class="string">"helloParent"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  Vue.component(<span class="string">'my-component2'</span>, &#123;</span></span><br><span class="line"><span class="xml">    template: `<span class="tag">&lt;<span class="name">div</span> <span class="attr">att</span>=<span class="string">"helloChild"</span>&gt;</span>子组件原有的特性被覆盖了<span class="tag">&lt;/<span class="name">div</span>&gt;</span>`</span></span><br><span class="line">  &#125;)</span><br><span class="line"><span class="javascript">  <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">    el: <span class="string">'#app3'</span></span></span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>上面渲染的结果是，<code>div</code>的<code>att</code>属性是<code>helloParent</code>。</li>
<li>注意：前面已经提到过，覆盖原则对于<code>class</code>和<code>style</code>不适用,而是采用了合并(<code>merge</code>)的原则</li>
</ul>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app3"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">my-component2</span> <span class="attr">att</span>=<span class="string">"helloParent"</span> <span class="attr">class</span>=<span class="string">"class2"</span> <span class="attr">style</span>=<span class="string">"color: red;"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  Vue.component(<span class="string">'my-component2'</span>, &#123;</span></span><br><span class="line"><span class="xml">    template: `<span class="tag">&lt;<span class="name">div</span> <span class="attr">att</span>=<span class="string">"helloChild"</span> <span class="attr">class</span>=<span class="string">"class1"</span> <span class="attr">style</span>=<span class="string">"background: yellow;"</span>&gt;</span>子组件原有的特性被覆盖了<span class="tag">&lt;/<span class="name">div</span>&gt;</span>`</span></span><br><span class="line">  &#125;)</span><br><span class="line"><span class="javascript">  <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">    el: <span class="string">'#app3'</span></span></span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>上面的渲染结果是，<code>div</code>的类名是<code>class1 class2</code>，行内样式是<code>color:red; background:yellow;</code></p>
</blockquote>
<h2 id="五、自定义事件"><a href="#五、自定义事件" class="headerlink" title="五、自定义事件"></a>五、自定义事件</h2><blockquote>
<p>通过<code>prop</code>属性，父组件可以向子组件传递数据，而子组件的自定义事件就是用来将内部的数据报告给父组件的</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app3"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">my-component2</span> <span class="attr">v-on:myclick</span>=<span class="string">"onClick"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  Vue.component(<span class="string">'my-component2'</span>, &#123;</span></span><br><span class="line"><span class="xml">    template: `<span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">"button"</span> @<span class="attr">click</span>=<span class="string">"childClick"</span>&gt;</span>点击我触发自定义事件<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span>`,</span></span><br><span class="line">    methods: &#123;</span><br><span class="line">      childClick () &#123;</span><br><span class="line"><span class="javascript">        <span class="keyword">this</span>.$emit(<span class="string">'myclick'</span>, <span class="string">'这是我暴露出去的数据'</span>, <span class="string">'这是我暴露出去的数据2'</span>)</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="javascript">  <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">    el: <span class="string">'#app3'</span>,</span></span><br><span class="line">    methods: &#123;</span><br><span class="line">      onClick () &#123;</span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(<span class="built_in">arguments</span>)</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>子组件在自己的方法中将自定义事件以及需要发出的数据通过以下代码发送出去</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">this.$emit(&apos;myclick&apos;, &apos;这是我暴露出去的数据&apos;, &apos;这是我暴露出去的数据2&apos;)</span><br></pre></td></tr></table></figure>
<ul>
<li>第一个参数是自定义事件的名字</li>
<li>后面的参数是依次想要发送出去的数据</li>
</ul>
<blockquote>
<p>父组件利用<code>v-on</code>为事件绑定处理器</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;my-component2 v-on:myclick=&quot;onClick&quot;&gt;&lt;/my-component2&gt;</span><br></pre></td></tr></table></figure>
<h3 id="5-1-绑定原生事件"><a href="#5-1-绑定原生事件" class="headerlink" title="5.1 绑定原生事件"></a>5.1 绑定原生事件</h3><blockquote>
<p>如果想在某个组件的根元素上监听一个原生事件。可以使用 <code>.native</code>修饰 <code>v-on</code></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">my-component</span> <span class="attr">v-on:click.native</span>=<span class="string">"doTheThing"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="5-2-探究v-model"><a href="#5-2-探究v-model" class="headerlink" title="5.2 探究v-model"></a>5.2 探究v-model</h3><blockquote>
<p><code>v-model</code>可以对表单控件实现数据的双向绑定，它的原理就是利用了绑定属性和事件来实现的。比如<code>input</code>控件。不使用<code>v-model</code>，可以这样实现数据的双向绑定</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app4"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">v-bind:value</span>=<span class="string">"text"</span> <span class="attr">v-on:input</span>=<span class="string">"changeValue($event.target.value)"</span>&gt;</span></span><br><span class="line">    &#123;&#123;text&#125;&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">        el: <span class="string">'#app4'</span>,</span></span><br><span class="line">        data: &#123;</span><br><span class="line"><span class="javascript">          text: <span class="string">'444'</span></span></span><br><span class="line">        &#125;,</span><br><span class="line">        methods: &#123;</span><br><span class="line">          changeValue (value) &#123;</span><br><span class="line"><span class="javascript">            <span class="keyword">this</span>.text = value</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>上面的代码同样实现了数据的双向绑定。其本质就是:</p>
</blockquote>
<ul>
<li>把<code>input</code>的<code>value</code>特性绑定到<code>Vue</code>实例的属性<code>text</code>上，<code>text</code>改变，<code>input</code>中的内容也会改变</li>
<li>然后把表单的<code>input</code>事件处理函数设置为<code>Vue</code>实例的一个方法，这个方法会根据输入参数改变<code>Vue</code>中<code>text</code>的值<br>相应的，在input中输入内容时，触发了<code>input</code>事件，把<code>event.target.value</code>传给这个方法，最后就实现了改变绑定的数据的效果。</li>
<li>而<code>v-model</code>就是上面这种方式的语法糖，也就是把上面的写法封装了一下，方便我们使用</li>
</ul>
<h3 id="5-3-使用自定义事件创建自定义的表单输入组件"><a href="#5-3-使用自定义事件创建自定义的表单输入组件" class="headerlink" title="5.3 使用自定义事件创建自定义的表单输入组件"></a>5.3 使用自定义事件创建自定义的表单输入组件</h3><blockquote>
<p>理解了<code>v-model</code>的内幕，也就可以把这个效果用在自定义表单组件上了。来实现一个简单的只能输入hello的表单输入组件</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app5"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">my-component3</span> <span class="attr">v-model</span>=<span class="string">"hello"</span>&gt;</span><span class="tag">&lt;/<span class="name">my-component3</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span>&#123;&#123;hello&#125;&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">  Vue.component(<span class="string">'my-component3'</span>, &#123;</span></span><br><span class="line"><span class="xml">    template: `<span class="tag">&lt;<span class="name">input</span> <span class="attr">ref</span>=<span class="string">"input"</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">:value</span>=<span class="string">"value"</span> @<span class="attr">input</span>=<span class="string">"checkInput($event.target.value)"</span>&gt;</span>`,</span></span><br><span class="line"><span class="javascript">    props: [<span class="string">'value'</span>],</span></span><br><span class="line">    methods: &#123;</span><br><span class="line">      checkInput (value) &#123;</span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> hello = <span class="string">'hello'</span></span></span><br><span class="line">        if (!hello.includes(value)) &#123;</span><br><span class="line"><span class="javascript">          <span class="keyword">this</span>.$emit(<span class="string">'input'</span>, hello)</span></span><br><span class="line"><span class="javascript">          <span class="keyword">this</span>.$refs.input.value = hello</span></span><br><span class="line"><span class="javascript">        &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="javascript">          <span class="keyword">this</span>.$emit(<span class="string">'input'</span>, value)</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="javascript">  <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">    el: <span class="string">'#app5'</span>,</span></span><br><span class="line">    data: &#123;</span><br><span class="line"><span class="javascript">      hello: <span class="string">''</span></span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="六、动态组件"><a href="#六、动态组件" class="headerlink" title="六、动态组件"></a>六、动态组件</h2><blockquote>
<p>通过使用保留的 <code>&lt;component&gt;</code> 元素，动态地绑定到它的 <code>is</code> 特性，可以让多个组件使用同一个挂载点，并动态切换</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app6"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">v-model</span>=<span class="string">"currentComponent"</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"home"</span>&gt;</span>home<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"post"</span>&gt;</span>post<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"about"</span>&gt;</span>about<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">component</span> <span class="attr">:is</span>=<span class="string">"currentComponent"</span>&gt;</span><span class="tag">&lt;/<span class="name">component</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">        el: <span class="string">'#app6'</span>,</span></span><br><span class="line">        data: &#123;</span><br><span class="line"><span class="javascript">          currentComponent: <span class="string">'home'</span></span></span><br><span class="line">        &#125;,</span><br><span class="line">        components: &#123;</span><br><span class="line">          home: &#123;</span><br><span class="line"><span class="xml">            template: `<span class="tag">&lt;<span class="name">header</span>&gt;</span>这是home组件<span class="tag">&lt;/<span class="name">header</span>&gt;</span>`</span></span><br><span class="line">          &#125;,</span><br><span class="line">          post: &#123;</span><br><span class="line"><span class="xml">            template: `<span class="tag">&lt;<span class="name">header</span>&gt;</span>这是post组件<span class="tag">&lt;/<span class="name">header</span>&gt;</span>`</span></span><br><span class="line">          &#125;,</span><br><span class="line">          about: &#123;</span><br><span class="line"><span class="xml">            template: `<span class="tag">&lt;<span class="name">header</span>&gt;</span>这是about组件<span class="tag">&lt;/<span class="name">header</span>&gt;</span>`</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>保留切换出去的组件，避免重新渲染</strong></p>
<blockquote>
<p>如果把切换出去的组件保留在内存中，可以保留它的状态或避免重新渲染。为此可以添加一个 <code>keep-alive</code> 指令参数</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">keep-alive</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">component</span> <span class="attr">:is</span>=<span class="string">"currentComponent"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 非活动组件将被缓存！ --&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">component</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">keep-alive</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="七、使用slot分发内容"><a href="#七、使用slot分发内容" class="headerlink" title="七、使用slot分发内容"></a>七、使用slot分发内容</h2><h3 id="7-1-单个slot"><a href="#7-1-单个slot" class="headerlink" title="7.1 单个slot"></a>7.1 单个slot</h3><blockquote>
<p>很多组件的使用方式是这样的：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;component&gt;&lt;/component&gt;</span><br></pre></td></tr></table></figure>
<ul>
<li>也就是说组件中是空的，没有放置任何文本或元素。但是原生的html元素都是可以进行嵌套的，<code>div</code>里面放<code>table</code>什么的。自定义组件开闭标签之间也可以放置内容，不过需要在定义组件时使用<code>slot</code>。</li>
<li><code>slot</code>相当于子组件设置了一个地方，如果在调用它的时候，往它的开闭标签之间放了东西，那么它就把这些东西放到<code>slot</code>中。</li>
<li>当子组件中没有<code>slot</code>时，父组件放在子组件标签内的东西将被丢弃；</li>
<li>子组件的<code>slot</code>标签内可以放置内容，当父组件没有放置内容在子组件标签内时，slot中的内容会渲染出来</li>
</ul>
<p>子组件的模板:</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">h2</span>&gt;</span>我是子组件的标题<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">slot</span>&gt;</span></span><br><span class="line">    只有在没有要分发的内容时才会显示。</span><br><span class="line">  <span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>父组件模板：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">h1</span>&gt;</span>我是父组件的标题<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">my-component</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>这是一些初始内容<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">my-component</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>渲染结果：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">h1</span>&gt;</span>我是父组件的标题<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span>&gt;</span>我是子组件的标题<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>这是一些初始内容<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="7-2-带名称的slot"><a href="#7-2-带名称的slot" class="headerlink" title="7.2 带名称的slot"></a>7.2 带名称的slot</h3><ul>
<li><code>slot</code>可以有很多个。那么子组件对于父组件放置的多余的内容如何放到各个<code>slot</code>中呢？方法就是子组件给每个<code>slot</code>起一个名字<code>name</code>，父组件放置多余的元素时，给每个元素的<code>slot</code>属性分配一个代表<code>slot</code>的名字。到时候，多余的内容就会根据自己的<code>slot</code>属性去找具有对应名字的<code>slot</code>元素。</li>
</ul>
<p><strong>注意</strong>：</p>
<ul>
<li>子组件可以有一个匿名的<code>slot</code>，当分发的多余内容找不到对应的<code>slot</code>时，就会进入这里面<br>如果子组件没有匿名的<code>slot</code>，当分发的多余内容找不到对应的<code>slot</code>时，就会被丢弃</li>
</ul>
<p>例如，假定我们有一个 <code>app-layout</code>组件，它的模板为：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">header</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">slot</span> <span class="attr">name</span>=<span class="string">"header"</span>&gt;</span><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">header</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">main</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">slot</span>&gt;</span><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">main</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">footer</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">slot</span> <span class="attr">name</span>=<span class="string">"footer"</span>&gt;</span><span class="tag">&lt;/<span class="name">slot</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">footer</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>父组件模版：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">app-layout</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">h1</span> <span class="attr">slot</span>=<span class="string">"header"</span>&gt;</span>这里可能是一个页面标题<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>主要内容的一个段落。<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>另一个主要段落。<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">p</span> <span class="attr">slot</span>=<span class="string">"footer"</span>&gt;</span>这里有一些联系信息<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">app-layout</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>渲染结果为：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">header</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>这里可能是一个页面标题<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">header</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">main</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>主要内容的一个段落。<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>另一个主要段落。<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">main</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">footer</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>这里有一些联系信息<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">footer</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="八、组件通信"><a href="#八、组件通信" class="headerlink" title="八、组件通信"></a>八、组件通信</h2><blockquote>
<p>在 Vue.js 中，父子组件的关系可以总结为 <code>props down</code>, <code>events up</code></p>
</blockquote>
<p><img src="https://upload-images.jianshu.io/upload_images/1480597-a057513f8294a129.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<blockquote>
<p>父组件通过 <code>props</code> 向下传递数据给子组件，子组件通过 <code>events</code> 给父组件发送消息</p>
</blockquote>
<h2 id="8-1-父与子"><a href="#8-1-父与子" class="headerlink" title="8.1 父与子"></a>8.1 父与子</h2><blockquote>
<p>父组件是通过<code>props</code>属性给子组件通信的来看下代码</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;parent&gt;</span><br><span class="line">    &lt;child :child-com=&quot;content&quot;&gt;&lt;/child&gt; //注意这里用驼峰写法哦</span><br><span class="line">&lt;/parent&gt;</span><br><span class="line"></span><br><span class="line">data()&#123;</span><br><span class="line">    return &#123;</span><br><span class="line">        content:&apos;sichaoyun&apos;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>子组件通过props来接受数据</strong></p>
<blockquote>
<p>第一种方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">props: [<span class="string">'childCom'</span>]</span><br></pre></td></tr></table></figure>
<blockquote>
<p>第二种方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">props: &#123;</span><br><span class="line">    childCom: <span class="built_in">String</span> <span class="comment">// 这里指定了字符串类型，如果类型不一致会警告哦</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>第三种方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">props: &#123;</span><br><span class="line">    childCom: &#123;</span><br><span class="line">        type: <span class="built_in">String</span>,</span><br><span class="line">        <span class="keyword">default</span>: <span class="string">'sichaoyun'</span> </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="8-2-子与父"><a href="#8-2-子与父" class="headerlink" title="8.2 子与父"></a>8.2 子与父</h3><blockquote>
<p><code>vue2.0</code>只允许单向数据传递，我们通过出发事件来改变组件的数据</p>
</blockquote>
<p>子组件</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> @<span class="attr">click</span>=<span class="string">"open"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line">    methods: &#123;</span><br><span class="line">       open() &#123;</span><br><span class="line"><span class="javascript">            <span class="keyword">this</span>.$emit(<span class="string">'showbox'</span>,<span class="string">'the msg'</span>); <span class="comment">//触发showbox方法，'the msg'为向父组件传递的数据</span></span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>父组件</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">child</span> @<span class="attr">showbox</span>=<span class="string">"toshow"</span> <span class="attr">:msg</span>=<span class="string">"msg"</span>&gt;</span><span class="tag">&lt;/<span class="name">child</span>&gt;</span> //监听子组件触发的showbox事件,然后调用toshow方法</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line">    methods: &#123;</span><br><span class="line">        toshow(msg) &#123;</span><br><span class="line"><span class="javascript">            <span class="keyword">this</span>.msg = msg;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="8-3-兄弟组件之间的通信"><a href="#8-3-兄弟组件之间的通信" class="headerlink" title="8.3 兄弟组件之间的通信"></a>8.3 兄弟组件之间的通信</h3><blockquote>
<p>我们可以实例化一个vue实例，相当于一个第三方</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">let vm = new Vue(); //创建一个新实例</span><br></pre></td></tr></table></figure>
<p>组件他哥</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;div @click=&quot;ge&quot;&gt;&lt;/div&gt;</span><br><span class="line">methods: &#123;</span><br><span class="line">    ge() &#123;</span><br><span class="line">        vm.$emit(&apos;blur&apos;,&apos;sichaoyun&apos;); //触发事件</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>组件小弟接受大哥命令</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">&lt;div&gt;&lt;/div&gt;</span><br><span class="line">created() &#123;</span><br><span class="line">  vm.$on(&apos;blur&apos;, (arg) =&gt; &#123; </span><br><span class="line">        this.test= arg; // 接收</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/08/27/vue-filters/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/08/28/vue-computed-watch/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
